/*
 *    Copyright 2008-2009 The Digester Tools Team
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.digestertools.annotations.reflect;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;

/**
 * 
 * @author Simone Tripodi
 * @version $Id: MethodArgument.java 357 2009-12-16 20:00:36Z simone.tripodi $
 */
public final class MethodArgument implements AnnotatedElement {

    private final int index;

    private final Class<?> parameterType;

    private final Annotation[] annotations;

    private final Method parentMethod;

    /**
     * 
     * @param index
     * @param parameterType
     * @param annotations
     * @param parentMethod
     */
    public MethodArgument(int index, Class<?> parameterType, Annotation[] annotations, Method parentMethod) {
        this.index = index;
        this.parameterType = parameterType;
        this.annotations = annotations;
        this.parentMethod = parentMethod;
    }

    /**
     * 
     * @return
     */
    public int getIndex() {
        return this.index;
    }

    /**
     * 
     * @return
     */
    public Class<?> getParameterType() {
        return this.parameterType;
    }

    /**
     * 
     * @return
     */
    public Method getParentMethod() {
        return this.parentMethod;
    }

    /**
     * {@inheritDoc}
     */
    public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
        for (Annotation annotation : this.annotations) {
            if (annotationType == annotation.annotationType()) {
                return annotationType.cast(annotation);
            }
        }
        return null;
    }

    /**
     * {@inheritDoc}
     */
    public Annotation[] getAnnotations() {
        return this.annotations;
    }

    /**
     * {@inheritDoc}
     */
    public Annotation[] getDeclaredAnnotations() {
        return this.annotations;
    }

    /**
     * {@inheritDoc}
     */
    public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
        for (Annotation annotation : this.annotations) {
            if (annotationType == annotation.annotationType()) {
                return true;
            }
        }
        return false;
    }

}
